package com.hydra.framework.router;

import android.util.Log;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.hydra.framework.router.interceptor.IRouterInterceptor;
import com.hydra.framework.router.receiver.RouterReceiver;

/**
 * Created by hasee on 2017/7/28.
 *
 * 后续要是还需要加一些乱七八糟的跳转，比如
 * 1、跳转到fragment
 * 2、调用Module
 * 3、加上callback
 * 4、加入action和category等
 * 就在RouterIntent和RouterReceiver + RouterAnnotation上面做文章就可以了
 * 也可以加入继承RouterIntent和RouterReceiver之类的，但是我觉得没啥必要
 */
public class JRouter {

    public static final String JROUTER_LOG_TAG = JRouter.class.getSimpleName();

    private static final String MODULE_ROUTER_CLASS_PACKAGE_NAME = "com.hydra.framework.router";

    private static final String MODULE_ROUTER_CLASS_NAME_SUFFIX = "$JRouter";
    private static final String MODULE_ROUTER_INTERCEPTOR_CLASS_NAME_SUFFIX = "$JRouterInterceptor";

    private static JRouterDispatcher sRouterDispatcher = new JRouterDispatcher();

    @MainThread
    public static void init() {
        String[] modules = JRouterModuleInfo.ALL_MODULES.split(",");

        for (String module : modules) {
            try {
                Class.forName(MODULE_ROUTER_CLASS_PACKAGE_NAME + "."
                        + module + MODULE_ROUTER_CLASS_NAME_SUFFIX);

                Class.forName(MODULE_ROUTER_CLASS_PACKAGE_NAME + "."
                        + module + MODULE_ROUTER_INTERCEPTOR_CLASS_NAME_SUFFIX);
            } catch (ClassNotFoundException e) {
                Log.w(JROUTER_LOG_TAG, "module " + module + " don't have router");
            }
        }
    }

    //这些add或者remove的path或者说url，只能是原始url，都不能带如 ?xx=1&yy=2 之类的参数，不会去解析这些url里的参数
    //跳转里的可以带参数
    public static void addRouter(@NonNull Class<?> receiverClass, int flags, int priority, @NonNull String... paths) {
        sRouterDispatcher.addRouter(receiverClass, flags, priority, paths);
    }

    public static void addRouter(RouterReceiver receiver,@NonNull String... urls) {
        for(String url : urls) {
            sRouterDispatcher.addRouter(url, receiver);
        }
    }

    public static void addInterceptor(@NonNull IRouterInterceptor interceptor, int priority, @NonNull String... paths) {
        sRouterDispatcher.addInterceptor(interceptor, priority, paths);
    }

    public static void removeInterceptor(@NonNull IRouterInterceptor interceptor, @Nullable String... paths) {
        sRouterDispatcher.removeInterceptor(interceptor, paths);
    }

    public static void removeAllInterceptorOfUrl(@NonNull String url) {
        sRouterDispatcher.removeInterceptorsOfUrl(url);
    }

    public static void removeRouter(@NonNull Class<?> receiverClass, @Nullable String... paths) {
        sRouterDispatcher.removeRouter(receiverClass, paths);
    }

    public static void removeAllRouterOfUrl(@NonNull String url) {
        sRouterDispatcher.removeRoutersOfUrl(url);
    }

    @MainThread
    public static void go(RouterIntent routerIntent) {
        sRouterDispatcher.notifyEvent(routerIntent);
    }

    public static RouterIntent build(String url) {
        return new RouterIntent(url);
    }
}
